package com.sniper.springmvc.jyzx;

import com.sniper.springmvc.model.JyzxReceive;
import com.sniper.springmvc.utils.FilesUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;

/**
 * 数据接收处理
 * Created by suzhen on 2017/4/3.
 */
public class JyzxUtils {


    /**
     * 解析反馈结果
     *
     * @param str
     */
    public static JyzxData decodeStr(String str) throws DocumentException {

        if (str == null || str.trim().length() == 0) {
            return new JyzxData("500", "结果为空");
        }

        Document document = DocumentHelper.parseText(str);
        //获取root
        Element root = document.getRootElement();
        // PackageHead
        Element packageHead = root.element("PackageHead");
        Element codeElement = packageHead.element("error");
        if (codeElement != null) {
            String code = codeElement.getStringValue();
            Element msg = packageHead.element("description");
            return new JyzxData(code, msg.getStringValue());
        }
        JyzxData result = new JyzxData();

        invoke(packageHead, result);
        //下旬头部
//        for (Iterator iter = packageHead.elementIterator(); iter.hasNext(); ) {
//            Element pe = (Element) iter.next();
//            String filed = pe.getName().toLowerCase();
//            filed = filed.substring(0, 1).toUpperCase() + filed.substring(1);
//            filed = "set" + filed;
//            Method method;
//            try {
//                method = result.getClass().getMethod(filed, String.class);
//                if (method != null) {
//                    method.invoke(result, pe.getStringValue());
//                }
//            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
//                //不做处理
//            }
//        }

        //查询数据
        Element dataElement = root.element("Data");
        //遍历元素
        for (Iterator iter = dataElement.elementIterator(); iter.hasNext(); ) {
            Element e = (Element) iter.next();
            JyzxReceive m = new JyzxReceive();
            m.setId(FilesUtil.UUID());

            invoke(e, m);
//            for (Iterator iter2 = e.elementIterator(); iter2.hasNext(); ) {
//                Element e2 = (Element) iter2.next();
//                String filed = e2.getName().toLowerCase();
//                filed = filed.substring(0, 1).toUpperCase() + filed.substring(1);
//                filed = "set" + filed;
//                Method method;
//                try {
//                    method = m.getClass().getMethod(filed, String.class);
//                    if (method != null) {
//                        method.invoke(m, e2.getStringValue());
//                    }
//                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e1) {
//                    // 不做处理
//                }
//            }
            result.getModels().add(m);
        }
        return result;
    }

    private static void invoke(Element e, Object m) {
        for (Iterator iter2 = e.elementIterator(); iter2.hasNext(); ) {
            Element e2 = (Element) iter2.next();
            String filed = e2.getName().toLowerCase();
            filed = filed.substring(0, 1).toUpperCase() + filed.substring(1);
            filed = "set" + filed;
            Method method = null;
            try {
                method = m.getClass().getMethod(filed, String.class);
                if (method != null) {
                    method.invoke(m, e2.getStringValue());
                }
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e1) {
                //不错处理
            }

        }
    }

}

